<template>
	<view class="login">
		<view class="top">
			<uv-navbar title="注册" height="44px" titleStyle="{font-weight: bold;font-size: 1.1rem;}" bgColor="#dff8f7"
				@leftClick="leftClick"></uv-navbar>
		</view>
		<view class="userlogin">
			<view class="userlogin1">
				<uv-input v-model="from.username" placeholder="请输入您的手机号" customStyle="border:none;padding: 2%;"
					@change="change"></uv-input>
			</view>
			<view class="userlogin1">
				<uv-input v-model="from.password" placeholder="请输入您的密码" customStyle="border:none;padding: 2%;"
					@change="change"></uv-input>
			</view>
			<view class="userlogin1">
				<uv-input v-model="from.password" placeholder="确认密码" customStyle="border:none;padding: 2%;"
					@change="change"></uv-input>
			</view>
			<view class="userlogin1">
				<uv-input v-model="from.captcha" placeholder="请输入验证码" customStyle="border:none;padding: 2%;">
					<!-- vue3模式下必须使用v-slot:suffix -->
					<template v-slot:suffix>
						<uv-code ref="uCode" @change="codeChange" seconds="20" changeText="X秒重新获取"></uv-code>
						<uv-button @click="SendCaptcha" :text="tips" color="#008c8c" type="success" size="mini"></uv-button>
					</template>
				</uv-input>

			</view>
		</view>
		<view class="determine">
			<uv-button type="primary" color="#008c8c" shape="circle" text="注册" @click="register"></uv-button>
			<!-- <button class="btn">登录</button> -->
		</view>
	</view>
</template>

<script>
	import {
		myRequest
	} from '@/request/request.js'
	export default {
		data() {
			return {
				from: {
					username: '',
					password: '123123',
					captcha: ''
				},
				tips: '',
				value: '内容'

			}
		},
		methods: {
			// 发送验证码
			SendCaptcha() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.showToast({
							title: '验证码已发送',
							duration: 2000
						});

						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.showToast({
						title: '倒计时结束后再发送',
						duration: 2000
					});

				}
				myRequest('login/sms?server=1', function(err, data) {
					if (err) {
						uni.showToast({
							icon: 'none',
							title: "登录失败"
						})
					} else {
						uni.showToast({
							title: "登录成功"
						})
						const captcha = data.data
						console.log("成功", captcha)
					}
				}, 'POST', {
					'mobile': this.from.username,


				})
			},
			// 注册接口
			register() {
				if (this.from.captcha == '') {
					uni.showToast({
						icon: 'none',
						title: "请填写验证码"
					})
				}else{
					myRequest('login/register?server=1', function(err, data) {
						if (err) {
							console.log(err);
							uni.showToast({
								icon: 'none',
								title: "注册失败"
							})
					
						} else {
					
							uni.showToast({
								title: "注册成功"
							})
							// 跳转登录页
							uni.navigateTo({
								url: '/pages/login/login'
							})
							console.log("成功", data)
						}
					}, 'POST', {
						'mobile': this.from.username,
						'code': this.from.captcha,
						'password': this.from.password,
					
					})
				}
				
			},
			change(e) {
				console.log('change', e);
			},
			//返回login
			leftClick() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
				console.log('leftClick');
			},
			codeChange(text) {
				this.tips = text;
			},
		}
	}
</script>

<style lang="scss">
	@import url("/pages/registered/registered.css");
</style>